import 'package:flutter/material.dart';

import '../../config/kq_global.dart';
import '../../resources/kq_images.dart';
import '../../theme/kq_theme_manager.dart';
import '../../utils/text_input_format_utils.dart';
import '../textFiled/kq_textfiled.dart';

/// 步进输入框
class KqStepTextField extends StatefulWidget {
  /// 是否可编辑
  final bool editable;

  /// 默认数量，默认0
  final int defQty;

  /// 最小数量，默认0
  final int minQty;

  /// 输入变化回调
  final ValueChanged<int>? onChanged;

  const KqStepTextField({
    Key? key,
    this.editable = true,
    this.defQty = 0,
    this.minQty = 0,
    this.onChanged,
  }) : super(key: key);

  @override
  KqStepTextFieldState createState() => KqStepTextFieldState();
}

class KqStepTextFieldState extends State<KqStepTextField> {
  int? newValue;

  bool isEmpty = false;

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        /// 数量减
        InkWell(
          onTap: () => _onDec(),
          child: Image.asset(
            _decAble()
                ? KqImages.commonIcReduceNormal
                : KqImages.commonIcReduceDisabled,
            width: 32,
            height: 26,
            package: KqGlobal.packageName,
          ),
        ),
        const SizedBox(
          width: 4,
          height: 1,
        ),

        /// 数量输入框
        SizedBox(
          width: 46,
          child: KqTextFiled(
            inputType: TextInputType.number,
            multiline: false,
            maxLines: 1,
            enableClearButton: false,
            newValue: isEmpty ? "" : newValue?.toString(),
            editable: widget.editable,
            textAlign: TextAlign.center,
            fontSize: KqThemeManager.instance
                    .getConfig()
                    .formConfig
                    .contentFontSize ??
                16,
            defValue: widget.defQty.toString(),
            placeHolder: widget.minQty.toString(),
            inputFormatters: TextInputFormatUtils.integer(),
            onChanged: (value) => _onValueChanged(value),
          ),
        ),
        const SizedBox(
          width: 4,
          height: 1,
        ),

        /// 数量加
        InkWell(
          onTap: () => _onPlus(),
          child: Image.asset(
            KqImages.commonIcIncreaseNormal,
            width: 32,
            height: 26,
            package: KqGlobal.packageName,
          ),
        )
      ],
    );
  }

  /// 点击减号
  void _onDec() {
    var qty = newValue ?? widget.defQty;
    if (qty > widget.minQty) {
      _onValueChanged((qty - 1).toString());
    }
  }

  /// 点击加号
  void _onPlus() {
    var qty = newValue ?? widget.defQty;
    _onValueChanged((qty + 1).toString());
  }

  /// 输入框数值变化
  void _onValueChanged(String text) {
    setState(() {
      isEmpty = text.isEmpty;
      int newInt = text.isEmpty ? widget.minQty : int.parse(text);
      if (newInt < widget.minQty) {
        newInt = widget.minQty;
      }
      newValue = newInt;
      if (widget.onChanged != null) {
        widget.onChanged!(newValue!);
      }
    });
  }

  // 减号能不能点
  bool _decAble() {
    var qty = newValue ?? widget.defQty;
    return qty > widget.minQty;
  }
}
